package com.duxd.springsecurityfinal.springsecurityfinal.response;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author dong
 * @date 2022/1/14 0014
 */
public enum ResponseBuild {

    request_404(){
        @Override
        public Response withMessage(String message) {
            return new Response(ResponseCode.request_404.getCode(), message);
        }
    },
    request_success(){
        @Override
        public Response withMessage(String message) {
            return new Response(ResponseCode.request_success.getCode(), message);
        }
    },
    login_success(){
        @Override
        public Response withMessage(String message) {
            return new Response(ResponseCode.login_success.getCode(), message);
        }
    },
    login_failed(){
        @Override
        public Response withMessage(String message) {
            return new Response(ResponseCode.login_failed.getCode(), message);
        }
    },
    logout_success(){
        @Override
        public Response withMessage(String message) {
            return new Response(ResponseCode.logout_success.getCode(), message);
        }
    },
    auth_failed(){
        @Override
        public Response withMessage(String message) {
            return new Response(ResponseCode.auth_failed.getCode(), message);
        }
    },
    no_authority(){
        @Override
        public Response withMessage(String message) {
            return new Response(ResponseCode.no_authority.getCode(), message);
        }
    };

    private ResponseBuild() {
    }

    public abstract Response withMessage(String message);

    public <T> Response<T> data(T data){
        return this.withMessage(null).setData(data);
    }

    enum ResponseCode{

        // 404 requested resource is not available
        request_404(HttpServletResponse.SC_NOT_FOUND),
        request_success(200),

        login_success(1210),
        login_failed(1211),
        logout_success(1220),
        // 认证失败
        auth_failed(10000),
        // 没有权限
        no_authority(20000);

        private int code;

        private ResponseCode(int code) {
            this.code = code;
        }

        public Integer getCode() {
            return this.code;
        }
    }

}
